package com.jimmy.medical.util;

import java.net.URLEncoder;

import com.jimmy.medical.config.MessageProperties;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MobileUtils {
    @Autowired
    private MessageProperties messageProperties;

    /**
     * 短信推送
     * 返回状态值：成功返回Success 失败返回：Faild
     * 发送失败结果会带上失败消息
     *
     * @param mobile   目标电话号码，可多个，英文逗号隔开
     * @param content  消息内容
     * @param sendTime 定时发送时间，发送即时消息置为空
     */
    public String sendMessage(String mobile, String content, String sendTime) {
        String message = null;

        try {
            String messageContent = content.replaceAll("<br/>", " ") + "【言康健管】";
            String sendContent = URLEncoder.encode(messageContent.replaceAll("<br/>", " "), "UTF-8");//发送内容
            String url = messageProperties.getUrl() + "&userid=" + messageProperties.getUserId() + "&account=" + messageProperties.getUserName() + "&password=" + messageProperties.getPassword()+"&mobile="+mobile+"&content="+sendContent+"&checkcontent=1&sendTime="+sendTime;
            String result = HttpUtils.getGetData(url);

            saxResult(result);
        } catch (Exception e) {
            message = e.getMessage();
        }

        return message;
    }

    public void saxResult(String result) {
        try {
            Document document = DocumentHelper.parseText(result);
            Element returnsms = document.getRootElement();
            String returnstatus = returnsms.elementTextTrim("returnstatus");
            String message = returnsms.elementTextTrim("message");
        } catch (DocumentException e) {
        }
    }
}